﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Media.Imaging;
using System.IO;

namespace TinyCinema.Data
{
    public class Menu:Execution
    {
        public string ID { get; set; }
        string _Name;
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
                OnPropertyChanged("Name");
            } 
        }
        Decimal _Price;
        public Decimal Price
        {
            get { return _Price; }
            set
            {
                _Price = value;
                OnPropertyChanged("Price");
            }
        }
        public Image Photo { get; set; }
        public ObservableCollection<MenuOrder> MenuOrders { get; set; }
        public Menu()
        {
            Photo = new Image();
        }
        public Menu(string _ID)
        {
            Photo = new Image();
            ID = _ID;            
            PutTableIntoDataSet(Tables.Menu);
            var tbl = ds.Tables[Tables.Menu];
            var Row = tbl.Select(MenuTable.ID + "='" + ID + "'")[0];
            Name = Row[MenuTable.Name] as String;
            try { Price = (Decimal)Row[MenuTable.Price]; }
            catch { }
            try { Photo = new Image(Row[MenuTable.Photo] as String); }
            catch { }
        }
        public static ObservableCollection<Menu> GetAllMenu()
        {
            PutTableIntoDataSet(Tables.Menu);
            var tbl = ds.Tables[Tables.Menu];
            var Rows = tbl.Select();
            var Result = new ObservableCollection<Menu>();
            foreach (var Row in Rows)

            {
                var menu= new Menu(Row[MenuTable.ID] as String);
                Result.Add(menu);
            }
            return Result;
        }
        public void AddPhoto()
        {
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.Filter = "Image|*.jpg";
            if (dialog.ShowDialog().Value)
            {
                MemoryStream memoryStream = new MemoryStream();
                BitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(new BitmapImage(new Uri(dialog.FileName))));
                encoder.Save(memoryStream);
                var bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.StreamSource = memoryStream;
                bitmapImage.EndInit();
                Photo.DisplayImage = bitmapImage;
            }
        }
        public void Save()
        {
            #region Lưu thực đơn
            PutTableIntoDataSet(Tables.Menu);
            using (var tbl = ds.Tables[Tables.Menu])
            {
                
                if (ID == null)
                {
                    ID = IDGenerate(Tables.Menu);
                    var Row = tbl.NewRow();
                    Row[MenuTable.ID] = ID;
                    Row[MenuTable.Name] = Name;
                    Row[MenuTable.Price] = Price;
                    Row[MenuTable.Photo] = Photo.ToBase64String();
                    tbl.Rows.Add(Row);
                    SaveCommand(Tables.Menu);
                }
                else
                {
                    foreach (System.Data.DataRow Row in tbl.Rows)
                        if ((Row[MenuTable.ID] as String) == ID)
                        {
                            Row[MenuTable.Name] = Name;
                            Row[MenuTable.Price] = Price;
                            Row[MenuTable.Photo] = Photo.ToBase64String();
                            SaveCommand(Tables.Menu);
                            break;
                        }
                }
            }
#endregion
        }
        public void Delete()
        {
            using (var tbl = ds.Tables[Tables.Menu])
            {
                string Condition = MenuTable.ID + "='" + ID + "'";
                string Sql = "select * from CTHDTD where MaTD='" + ID + "'";
                if (Excute(Sql).Rows.Count>0) throw new Exception();
                tbl.Select(Condition)[0].Delete();
                SaveCommand(Tables.Menu);
            }
            
        }
        public static ObservableCollection<Menu> SearchByName(string str)
        {
            PutTableIntoDataSet(Tables.Menu);
            var tbl = ds.Tables[Tables.Menu];
            var Rows = tbl.Select(MenuTable.Name + " Like '%" + str + "%'");
            var Result = new ObservableCollection<Menu>();
            foreach (var Row in Rows)
            {
                Result.Add(new Menu(Row[MenuTable.ID] as String));
            }
            return Result;
        }
                
    }
}
